package order;

import java.util.HashMap;

import model.*;

import javax.swing.JOptionPane;

public abstract class ActionOrder extends Order{

    protected final Player player;
    protected final Territory source;
    protected final Territory destination;
    protected final HashMap<TechnologyLevel, Integer> troopAmount;
    
    @SuppressWarnings("unchecked")
	public ActionOrder(Territory source, Player player, Territory destination, HashMap<TechnologyLevel, Integer> troopAmount){
        this.source = source;
        this.player = player;
        this.destination = destination;
        this.troopAmount = (HashMap<TechnologyLevel, Integer>) troopAmount.clone();
    }

    public HashMap<TechnologyLevel, Integer> getTroopAmount(){return troopAmount;}
    public int getTroopAmount(TechnologyLevel level){return troopAmount.get(level);}
     
    public boolean checkOrder(Map m){
        if ((! checkTroop()) || (! checkPath(m)) || (! checkOwner())) return false;
        return true;       
    }
    protected boolean checkTroop(){
        for (TechnologyLevel level:TechnologyLevel.values())
        	if (troopAmount.containsKey(level))
    	      if (troopAmount.get(level) > source.getUsableTroopAmount(level, player)) {
    		    JOptionPane.showMessageDialog(null, "No enough" + level + "troops to move!");
    		    return false;
    	     }
    		  return true;
    }
    public Territory getSource(){return source;}
    public Territory getDestination(){return destination;}
    protected abstract boolean checkOwner();
    public  abstract String printOrder();
    protected abstract boolean checkPath(Map m);
    abstract public void executeOrder();
    abstract public void undoOrder();
}
